const mongoose = require("mongoose");

const TaskProgressSchema = new mongoose.Schema(
  {
    task: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Task",
      required: true,
    },
    user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      required: true,
    },
    // 进度百分比
    progress: {
      type: Number,
      required: true,
      min: 0,
      max: 100,
    },
    // 进度标题
    title: {
      type: String,
      required: true,
      trim: true,
      maxlength: 200,
    },
    // 进度描述
    description: {
      type: String,
      trim: true,
      maxlength: 2000,
      default: "",
    },
    // 完成的工作内容
    workDone: {
      type: String,
      trim: true,
      maxlength: 2000,
      default: "",
    },
    // 遇到的问题
    issues: {
      type: String,
      trim: true,
      maxlength: 1000,
      default: "",
    },
    // 下一步计划
    nextSteps: {
      type: String,
      trim: true,
      maxlength: 1000,
      default: "",
    },
    // 工时记录（小时）
    hoursWorked: {
      type: Number,
      min: 0,
      default: 0,
    },
    // 进度类型
    type: {
      type: String,
      enum: ["milestone", "update", "completion", "blocker"],
      default: "update",
    },
    // 是否为主要里程碑
    isMilestone: {
      type: Boolean,
      default: false,
    },
    // 附件列表
    attachments: {
      type: [mongoose.Schema.Types.ObjectId],
      ref: "TaskAttachment",
      default: [],
    },
    // 额外数据
    metadata: {
      type: mongoose.Schema.Types.Mixed,
      default: {},
    },
  },
  { timestamps: true }
);

// 索引优化
TaskProgressSchema.index({ task: 1, createdAt: -1 });
TaskProgressSchema.index({ user: 1, createdAt: -1 });
TaskProgressSchema.index({ task: 1, user: 1 });
TaskProgressSchema.index({ type: 1, isMilestone: 1 });

const TaskProgress = mongoose.model("TaskProgress", TaskProgressSchema);

module.exports = TaskProgress;
